<?php
    require_once "../../config.php";
    require_once "../../functions.php";
    //获取前端传来的参数\
    $currentPage = $_POST["currentPage"];
    $pageSize = $_POST["pageSize"];
    $offset = ($currentPage -1) * $pageSize;
    //连接数据库
    $connect = connect();
    //sql
    $querySql = "SELECT c.id,c.author,c.created,c.content,c.`status`,p.title FROM comments c 
                left JOIN posts p on p.id = c.post_id
                LIMIT {$offset},$pageSize";
    //执行sql
    $queryResult = querySelect($connect,$querySql); 
    //计算最大的页码数
    $countSql = "SELECT count(id) as countPage from comments";
    $countResult = querySelect($connect,$countSql);
    // print_r($countResult);
    // Array
    //     (
    //         [0] => Array
    //             (
    //                 [countPage] => 507
    //             )

    //     )
    //获取总共的数据条数
    $countPage = $countResult[0]["countPage"];
    //计算一共有多少页
    $pageNum = ceil($countPage / $pageSize);
    
    $response = ["code"=>0,"msg"=>"操作失败"];
    if($queryResult){
        $response["code"] = 1;
        $response["msg"] = "操作成功";
        $response["data"] = $queryResult;
        $response["pageNum"] = $pageNum;
    }
    header("content-type:application/json;charset=ut8");
    echo json_encode($response);
?>